package com.imooc.diveinspringboot.mybatis.service.impl;

import com.imooc.diveinspringboot.mybatis.entity.OrderEntity;
import com.imooc.diveinspringboot.mybatis.mapper.OrderEntityMapper;
import com.imooc.diveinspringboot.mybatis.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Chris
 * @version 1.0.0
 * @date 2019/05/14
 * @since 1.0.0
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderEntityMapper orderMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void parent() {

        try {
            System.out.println("parent: " + this.getClass().getName());
            AopUtils.
            child();
        } catch (Exception ignore) {

        }

        OrderEntity order = new OrderEntity();
        order.setTitle("测试订单parent");
        order.setOrderNo("parent");
        order.setStatus(0);
        order.setAmount(100);
        orderMapper.insert(order);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void child() {
        OrderEntity order = new OrderEntity();
        order.setTitle("测试订单child");
        order.setOrderNo("child");
        order.setStatus(0);
        order.setAmount(111);
        orderMapper.insert(order);
        throw new RuntimeException("child fail...");
    }
}
